package shellstation;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.Date;
import net.sourceforge.jcalendarbutton.JCalendarButton;

public class ReceiptView implements ActionListener{

    private int yesNo;

    private net.sourceforge.jcalendarbutton.JCalendarButton  jCalendar;

    private JPanel line;
    private JPanel panel2;
    private JPanel line2;

    private JLabel receiptNumberH;
    private JLabel product;
    private JLabel size;
    private JLabel quantity;
    private JLabel listOfProductsH;

    private JButton addProductButton;
    private JButton submitButton;
    private JButton removeProductButton;
    private JButton searchButton;

    private JTextField receiptNumberT;
    private JTextField productQuantity;

    private DefaultTableModel listOfProducts;
    private DefaultTableModel listOfP;

    private JTable table;
    private JTable productList;

    private JScrollPane scrollpane;
    private JScrollPane scrollpane2;

    private JComboBox productSize;

    private Color gray = new Color(182, 182, 182);

    private Icon addProductIcon;
    private Icon minusProductIcon;
    private Icon searchIcon;

    private String[] productName;
    private ArrayList listSize;
    private String[] productColumn ={"Product List"};

    private String[] columnName = {"Product", "Size", "Quantity"};

    private boolean uniqueReceipt= true;
//    private net.sourceforge.jcalendarbutton.JCalendarButton jCalendar;

    public void ReceiptView(){

        ReceiptModel r= new ReceiptModel();
        listSize = r.getlistSize();

        addProductIcon= new ImageIcon(getClass().getResource("image/addIcon.gif"));
        minusProductIcon= new ImageIcon(getClass().getResource("image/minusIcon.gif"));
        searchIcon= new ImageIcon(getClass().getResource("image/search.gif"));
        
        receiptNumberH = new JLabel("Receipt Number:");
        receiptNumberH.setBounds(195, 15, 125, 50);
       

        listOfProductsH = new JLabel("Receipt Details:");
        listOfProductsH.setBounds(195, 45, 135, 50);
       

        receiptNumberT = new JTextField(15);
        receiptNumberT.setBounds(305, 30, 100, 20);
        
        listOfProducts = new DefaultTableModel(13, 3);
        listOfProducts.setColumnIdentifiers(columnName);

        table = new JTable(listOfProducts);
        table.setPreferredScrollableViewportSize(new Dimension(100, 50));

        scrollpane = new JScrollPane(table);
        scrollpane.setBounds(185, 90, 245, 220);

        line2= new JPanel(null);
        line2.setBackground(Color.BLACK);
        line2.setBounds(180, 0, 1, 420);

        product= new JLabel("Product List");
        product.setBounds(275, 225, 100, 50);
        

        size= new JLabel ("Size");
        size.setBounds(25, 15, 100, 50);
        
        quantity= new JLabel("Quantity");
        quantity.setBounds(25, 320, 100, 50);


        productSize = new JComboBox(listSize.toArray());
        productSize.setBounds(73, 30, 60, 20);

        productQuantity = new JTextField(5);
        productQuantity.setBounds(90, 335, 50, 20);

        listOfP = new DefaultTableModel(14,1);
        listOfP.setColumnIdentifiers(productColumn);


        productList = new JTable(listOfP);
        productList.setPreferredScrollableViewportSize(new Dimension(100, 50));
        
        scrollpane2= new JScrollPane(productList);
        scrollpane2.setBounds(19, 70, 150, 240);

        addProductButton= new JButton("", addProductIcon);
        addProductButton.setBounds(148, 330, 25, 25);
        addProductButton.setBackground(null);
        addProductButton.addActionListener(this);

        removeProductButton= new JButton("",minusProductIcon);
        removeProductButton.setBounds(195, 330, 25, 25);
        removeProductButton.setBackground(null);
        removeProductButton.addActionListener(this);

        searchButton= new JButton("",searchIcon);
        searchButton.setBounds(148,25,25,25);
        searchButton.setBackground(null);
        searchButton.addActionListener(this);

        submitButton= new JButton("Submit");
        submitButton.setBounds(340, 330, 80,25);
        submitButton.addActionListener(this);
    }

    public JLabel getreceiptNumberH(){
        return receiptNumberH;
    }

    public JLabel getlistOfProductsH(){
        return listOfProductsH;
    }

    public JPanel getline2(){
        return line2;
    }

    public JLabel getProduct(){
        return product;
    }

    public JLabel getSize(){
        return size;
    }

    public JLabel getQuantity(){
        return quantity;
    }


    public JComboBox getproductSize(){
        return productSize;
    }

    public JTextField getproductQuantity(){
        return productQuantity;
    }

    public JTextField getreceiptNumberT(){
        return receiptNumberT;
    }

    public DefaultTableModel getlistOfProducts(){
        return listOfProducts;
    }

    public JButton getaddProductButton(){
        return addProductButton;
    }

    public JButton getSubmitButton(){
        return submitButton;
    }
    public JButton getSearchButton(){
        return searchButton;
    }
    public JButton getremoveProductButton(){
        return removeProductButton;
    }

    public JScrollPane getscrollpane(){
        return scrollpane;
    }

    public void setpanel(JPanel panel){
        panel2 = panel;
    }

    public JScrollPane getscrollpane2(){
        return scrollpane2;
    }

    public JTable getJTable(){
        return table;
    }

    public net.sourceforge.jcalendarbutton.JCalendarButton getCalendar(){
        return jCalendar;
    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource() == submitButton){

            String s=receiptNumberT.getText();
            
            if(s.equals(""))
                JOptionPane.showMessageDialog(null, "A field has been left blank");

            else if(listOfProducts.getValueAt(0, 0) == null)
                 JOptionPane.showMessageDialog(null, "Product List is Empty");

            else{

                yesNo = JOptionPane.showConfirmDialog(null, "Are you sure you want to add receipt?", null, JOptionPane.YES_NO_OPTION);
            
                if( yesNo == JOptionPane.YES_OPTION){

                  ReceiptModel r = new ReceiptModel();
                  uniqueReceipt =r.insertProduct(table, s, uniqueReceipt);
                  System.out.println(uniqueReceipt);
                  if(uniqueReceipt){
                    JOptionPane.showMessageDialog(null, "Receipt Added");
                    receiptNumberT.setText("");

                    int i=0;
                    while(listOfProducts.getValueAt(i, 0) != null){

                        listOfProducts.setValueAt(null, i, 0);
                        listOfProducts.setValueAt(null, i, 1);
                        listOfProducts.setValueAt(null, i, 2);

                        i++;
                    }

                  }
                }

            }
        }

        else if(e.getSource() == removeProductButton){
            try{
                int i =table.getSelectedRow();
               
                if( !listOfProducts.getValueAt(i, 0).toString().isEmpty() )
                    listOfProducts.removeRow(i);
            }
            
            catch(ArrayIndexOutOfBoundsException a){
                JOptionPane.showMessageDialog(null, "Nothing to be Removed");
                     
            }

            catch(NullPointerException n){
                JOptionPane.showMessageDialog(null, "Nothing to be Removed");
            }
       }

        else if(e.getSource() == addProductButton){
            boolean error=false;
            String s= productQuantity.getText();
            int i;

            while(true){
                try{
                    i= Integer.parseInt(s);
                    break;
                }

                catch(NumberFormatException nx){
                     JOptionPane.showMessageDialog(null, "Quantity should be a whole number");
                     error= true;
                     break;
                }
            }
             if(error != true){

                String size= productSize.getSelectedItem().toString() ;
                String quantity = productQuantity.getText().toString();
                int b=productList.getSelectedRow();
                
                try{
                    String product = productList.getValueAt(b, 0).toString();
                    String[] dataRow= {product, size, quantity};
                    System.out.println(product);

                    if(productList.getValueAt(b, 0) != " " || listOfP.getValueAt(b, 0) != null)
                        listOfProducts.insertRow(0, dataRow); 
                
                }
                
                catch(NullPointerException np){
                    JOptionPane.showMessageDialog(null, "Nothing to Add");
                }
                  
            }
        }

        else if( e.getSource() == searchButton){
            int a=0;
            while(a < listOfP.getRowCount()){
                  listOfP.setValueAt(" ", a, 0);
                  a++;
            }

            ReceiptModel rm = new ReceiptModel();
            ArrayList al= new ArrayList();
            al= rm.getProduct(productSize.getSelectedItem().toString());
            
            int i=0 ;
            while( i < al.size() ){
                listOfP.setValueAt(al.get(i).toString(), i, 0);
                i++;
            }
        }
    }
}
